package c.e.a.b.b;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.pm.Signature;
import android.os.StrictMode;
import android.os.StrictMode.ThreadPolicy;
import android.util.Log;
import c.e.a.b.b.h.r;
import c.e.a.b.b.k.b;
import java.util.Objects;
import javax.annotation.CheckReturnValue;

@CheckReturnValue
public class f {
    public static f b;
    public final Context a;

    public f(Context context) {
        this.a = context.getApplicationContext();
    }

    public static f a(Context context) {
        Objects.requireNonNull(context, "null reference");
        synchronized (f.class) {
            if (b == null) {
                r rVar = n.a;
                synchronized (n.class) {
                    if (n.c == null) {
                        n.c = context.getApplicationContext();
                    } else {
                        Log.w("GoogleCertificates", "GoogleCertificates has been initialized already");
                    }
                }
                b = new f(context);
            }
        }
        return b;
    }

    public static p c(PackageInfo packageInfo, p... pVarArr) {
        Signature[] signatureArr = packageInfo.signatures;
        if (signatureArr == null) {
            return null;
        }
        if (signatureArr.length != 1) {
            Log.w("GoogleSignatureVerifier", "Package has more than one signature.");
            return null;
        }
        int i = 0;
        q qVar = new q(packageInfo.signatures[0].toByteArray());
        while (i < pVarArr.length) {
            if (pVarArr[i].equals(qVar)) {
                return pVarArr[i];
            }
            i++;
        }
        return null;
    }

    public static boolean d(PackageInfo packageInfo, boolean z) {
        if (!(packageInfo == null || packageInfo.signatures == null)) {
            p c;
            if (z) {
                c = c(packageInfo, s.a);
            } else {
                c = c(packageInfo, s.a[0]);
            }
            if (c != null) {
                return true;
            }
        }
        return false;
    }

    public boolean b(int i) {
        w wVar;
        String[] packagesForUid = b.a(this.a).a.getPackageManager().getPackagesForUid(i);
        if (packagesForUid != null && packagesForUid.length != 0) {
            wVar = null;
            for (Object obj : packagesForUid) {
                ThreadPolicy allowThreadDiskReads;
                ThreadPolicy allowThreadDiskReads2;
                try {
                    PackageInfo packageInfo = b.a(this.a).a.getPackageManager().getPackageInfo(obj, 64);
                    boolean a = e.a(this.a);
                    if (packageInfo == null) {
                        wVar = w.b("null pkg");
                    } else if (packageInfo.signatures.length != 1) {
                        wVar = w.b("single cert required");
                    } else {
                        q qVar = new q(packageInfo.signatures[0].toByteArray());
                        String str = packageInfo.packageName;
                        r rVar = n.a;
                        allowThreadDiskReads = StrictMode.allowThreadDiskReads();
                        w a2 = n.a(str, qVar, a, false);
                        StrictMode.setThreadPolicy(allowThreadDiskReads);
                        if (a2.a) {
                            ApplicationInfo applicationInfo = packageInfo.applicationInfo;
                            if (!(applicationInfo == null || (applicationInfo.flags & 2) == 0)) {
                                allowThreadDiskReads2 = StrictMode.allowThreadDiskReads();
                                w a3 = n.a(str, qVar, false, true);
                                StrictMode.setThreadPolicy(allowThreadDiskReads2);
                                if (a3.a) {
                                    wVar = w.b("debuggable release cert app rejected");
                                }
                            }
                        }
                        wVar = a2;
                    }
                } catch (NameNotFoundException unused) {
                    String str2 = "no pkg ";
                    String valueOf = String.valueOf(obj);
                    wVar = w.b(valueOf.length() != 0 ? str2.concat(valueOf) : new String(str2));
                } catch (Throwable th) {
                    StrictMode.setThreadPolicy(allowThreadDiskReads2);
                }
                if (wVar.a) {
                    break;
                }
            }
        } else {
            wVar = w.b("no pkgs");
        }
        if (!wVar.a) {
            String str3 = "GoogleCertificatesRslt";
            if (Log.isLoggable(str3, 3)) {
                if (wVar.c != null) {
                    Log.d(str3, wVar.a(), wVar.c);
                } else {
                    Log.d(str3, wVar.a());
                }
            }
        }
        return wVar.a;
    }
}
